package mortal.game.control
{
    import Message.Game.*;
    import Message.Public.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;
    import modules.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.model.*;
    import mortal.game.mvc.*;
    import mortal.game.rules.*;
    import mortal.game.scene.events.*;
    import mortal.game.scene.layer.utils.*;
    import mortal.game.scene.map.*;
    import mortal.game.scene.player.*;
    import mortal.game.scene.player.entity.*;
    import mortal.game.view.pet.*;
    import mortal.game.view.petForge.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class PetForgeController extends Controller
    {
        private var _petForgeModule:PetForgeModule;
        private var _growthBlessWindow:PetGrowthBlessWindow;
        private var _spiritBlessWindow:PetSpiritBlessWindow;
        private var petUid:String = "";
        private var _friendOnline:Boolean = false;
        private static var toOpenTab:int = 0;

        public function PetForgeController()
        {
            Dispatcher.addEventListener(PetForgeEvent.UpdateDataProvider, this.onUpdateDataProvider);
            Dispatcher.addEventListener(PetForgeEvent.CancelTeamFeed, this.onCancelTeamFeed);
            Dispatcher.addEventListener(EventName.PetRefreshGrowth, this.onPetRefreshGrowth);
            Dispatcher.addEventListener(EventName.PetRefreshGrowthSuccess, this.onPetRefreshGrowthSuccess);
            Dispatcher.addEventListener(EventName.PetStrengthenSpirit, this.onPetStrengthenSpirit);
            Dispatcher.addEventListener(EventName.PetStrengthenSpiritSuccess, this.onPetStrengthenSpiritSuccess);
            Dispatcher.addEventListener(EventName.PetSingleSire, this.onPetSingleSire);
            Dispatcher.addEventListener(EventName.PetSingleSireSuccess, this.onPetSingleSireSuccess);
            Dispatcher.addEventListener(EventName.PetTeamSire, this.onPetTeamSire);
            Dispatcher.addEventListener(EventName.PetTeamSireSuccess, this.onPetTeamSireSuccess);
            Dispatcher.addEventListener(EventName.PetGetBackPet, this.onPetGetBackPet);
            Dispatcher.addEventListener(EventName.PetGetBackPetSuccess, this.onPetGetBackPetSuccess);
            Dispatcher.addEventListener(EventName.PetBagUpdate, this.onPetBagUpdateHandler);
            Dispatcher.addEventListener(EventName.OpenTeamFeed, this.onOpenTeamFeed);
            Dispatcher.addEventListener(EventName.Group_Downline, this.onGroupDownline);
            Dispatcher.addEventListener(EventName.Group_Online, this.onGroupOnline);
            Dispatcher.addEventListener(EventName.Group_Disband, this.onGroupDisband);
            Dispatcher.addEventListener(PetEventName.PetOpenGrowthBlessWindow, this.onPetOpenGrowthBlessWindowHandler);
            Dispatcher.addEventListener(PetEventName.PetOpenSpiritBlessWindow, this.onPetOpenSpiritBlessWindowHandler);
            Dispatcher.addEventListener(EventName.PetUseBless, this.onPetUseBlessHandler);
            Dispatcher.addEventListener(EventName.PetUseBlessSuccess, this.onPetUseBlessSuccessHandler);
            
        }

        override protected function initServer() : void
        {
            super.initServer();
            NetDispatcher.addCmdListener(ServerCommand.BackpackDataChange, this.serverBagDataChangehandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.serverBagDataChangehandler);
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.onMoneyUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetSire_OperAgree, this.onPetSireOperAgreeHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetSire_OperUpdatePet, this.onPetSireOperUpdatePetHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetSire_OperLock, this.onPetSireOperLockHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetSire_OperConfirm, this.onPetSireOperConfirmHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetSire_OperCancel, this.onPetSireOperCancelHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetSire_OperEnd, this.onPetSireOperEndHandler);
            
        }

        override protected function initView() : IView
        {
            if (this._petForgeModule == null)
            {
                this._petForgeModule = new PetForgeModule();
                this._petForgeModule.addEventListener(Event.ADDED_TO_STAGE, this.onAddedToStageHandler);
            }
            return this._petForgeModule;
        }

        override public function get view() : IView
        {
            return this.initView();
        }

        private function onAddedToStageHandler(event:Event) : void
        {
            NpcEffectRule.registDialogEffectWindow(this.view);
            this.setDefaultTab(toOpenTab);
            this._petForgeModule.checkGuide();
            
        }

        private function onAddedToStageGuideHandler(event:Event) : void
        {
            this._petForgeModule.checkGuide();
            
        }

        public function setDefaultTab(param1:int) : void
        {
            this._petForgeModule.setDefault(param1);
            
        }

        private function onUpdateDataProvider(event:DataEvent) : void
        {
            var _loc_2:Array = [];
            var _loc_3:* = event.data as String;
            if (_loc_3 == PetForgeType.petRefine)
            {
                _loc_2 = cache.pet.petInfos;
            }
            else if (_loc_3 == PetForgeType.upgrade)
            {
                _loc_2 = cache.pet.petInfos;
            }
            else if (_loc_3 == PetForgeType.skillStudy)
            {
                _loc_2 = cache.pet.petInfos;
            }
            else if (_loc_3 == PetForgeType.singleBreed)
            {
                _loc_2 = cache.pet.petInfos;
            }
            else if (_loc_3 == PetForgeType.teamBreed)
            {
                _loc_2 = cache.pet.petInfos;
            }
            this._petForgeModule.updatePetListProvider(_loc_2);
            if (PetForgeModule.currentMaterialType == PetForgeModule.MaterialType_Use)
            {
                this._petForgeModule.updateUseMaterialDataProvider();
            }
            else if (PetForgeModule.currentMaterialType == PetForgeModule.MaterialType_Buy)
            {
                this._petForgeModule.updateBuyMaterialDataProvider();
            }
            
        }

        public function onPetRefreshGrowth(event:DataEvent) : void
        {
            var _loc_2:* = event.data["uid"] as String;
            var _loc_3:* = event.data["materials"] as Array;
            GameProxy.petProxy.refreshGrowth(_loc_2, _loc_3);
            if (cache.guide.guidePetRefresh)
            {
                GameController.pet.view.hide();
            }
            
        }

        public function onPetRefreshGrowthSuccess(event:DataEvent) : void
        {
            var _loc_2:* = new Dictionary();
            var _loc_3:* = event.data["success"] as Boolean;
            if (_loc_3)
            {
                MsgManager.addBroadCast("宠物成长提升成功");
            }
            else
            {
                MsgManager.addBroadCast("宠物成长提升失败");
            }
            
        }

        public function onPetStrengthenSpirit(event:DataEvent) : void
        {
            var _loc_2:* = event.data["uid"] as String;
            var _loc_3:* = event.data["materials"] as Array;
            GameProxy.petProxy.strengthenSpirit(_loc_2, _loc_3);
            
        }

        public function onPetStrengthenSpiritSuccess(event:DataEvent) : void
        {
            var _loc_2:* = new Dictionary();
            this.petUid = event.data["uid"] as String;
            var _loc_3:* = event.data["success"] as Boolean;
            if (_loc_3)
            {
                MsgManager.addBroadCast("宠物灵性提升成功");
            }
            else
            {
                MsgManager.addBroadCast("宠物灵性提升失败");
            }
            
        }

        private function onPetSingleSire(event:DataEvent) : void
        {
            var _loc_2:* = event.data["firstUid"] as String;
            var _loc_3:* = event.data["secondUid"] as String;
            var _loc_4:* = event.data["firstItem"] as String;
            var _loc_5:* = event.data["secondItem"] as String;
            GameProxy.petProxy.sirePet(_loc_2, _loc_3, _loc_4, _loc_5);
            
        }

        private function onPetSingleSireSuccess(event:DataEvent) : void
        {
            this._petForgeModule.singleFreedPanel.clearPanel();
            MsgManager.addBroadCast("已成功繁殖");
            
        }

        private function serverBagDataChangehandler(param1:Object = null) : void
        {
            var _loc_2:Dictionary = null;
            if (this._petForgeModule && this._petForgeModule.isHide == false)
            {
                if (PetForgeModule.currentMaterialType == PetForgeModule.MaterialType_Use)
                {
                    this._petForgeModule.updateUseMaterialDataProvider();
                }
                this._petForgeModule.autoSelectMaterial();
                if (PetForgeModule.currentPetInfo)
                {
                    _loc_2 = cache.pet.autoSelectMaterial(PetForgeModule.currentPetInfo, PetForgeModule.currentForgeType, this._petForgeModule.defCheckBox.selected);
                    if (PetForgeModule.currentForgeType == PetForgeType.petRefine)
                    {
                        this._petForgeModule.refinePanel.updateAutoMaterial(_loc_2["material"], _loc_2["charms"]);
                    }
                    else if (PetForgeModule.currentForgeType == PetForgeType.upgrade)
                    {
                        this._petForgeModule.upgradePanel.updateAutoMaterial(_loc_2["material"], _loc_2["charms"]);
                    }
                    else if (PetForgeModule.currentForgeType == PetForgeType.skillStudy)
                    {
                    }
                    else if (PetForgeModule.currentForgeType == PetForgeType.singleBreed)
                    {
                        this._petForgeModule.singleFreedPanel.setIntoMaterial();
                    }
                    else if (PetForgeModule.currentForgeType == PetForgeType.teamBreed)
                    {
                        this._petForgeModule.teamFreedPanel.setIntoMaterial();
                    }
                    this._petForgeModule.validateForgeCondition();
                }
            }
            
        }

        private function onMoneyUpdateHandler(param1:SMoney = null) : void
        {
            if (this._petForgeModule)
            {
                this._petForgeModule.validateForgeCondition();
            }
            
        }

        private function onPetBagUpdateHandler(event:DataEvent) : void
        {
            if (this._petForgeModule && this._petForgeModule.isHide == false)
            {
                this._petForgeModule.updatePetListProvider(cache.pet.petInfos);
            }
            
        }

        private function onOpenTeamFeed(event:DataEvent) : void
        {
            if (this.canOpenTeamSire())
            {
                toOpenTab = 3;
                this.view.show();
                Dispatcher.dispatchEvent(new DataEvent(EventName.PetTeamSire, {oper:EPetSireOperation.EPetSireOperApply, petSireId:"", uid:""}));
            }
            
        }

        private function onPetTeamSire(event:DataEvent) : void
        {
            var _loc_2:* = event.data["oper"] as EPetSireOperation;
            if (_loc_2 == EPetSireOperation.EPetSireOperApply)
            {
                if (this.isCanInvite() == false)
                {
                    
                }
                if (this.canOpenTeamSire() == false)
                {
                    
                }
                cache.pet.isCanSelectPetSire = true;
                this._petForgeModule.teamFreedPanel.clearPanel();
                MsgManager.addBroadCast("已发出繁殖邀请");
            }
            var _loc_3:* = event.data["uid"] as String;
            var _loc_4:* = this.getTeammateEntityId();
            var _loc_5:String = "";
            if (cache.pet.petSire)
            {
                _loc_5 = cache.pet.petSire.petSireId;
            }
            if (_loc_4)
            {
                GameProxy.petProxy.groupPetSire(_loc_2, _loc_4, _loc_5, _loc_3);
            }
            
        }

        private function isCanInvite() : Boolean
        {
            if (cache.group.captain)
            {
                if (EntityUtil.toString(cache.role.entityInfo.entityId) != EntityUtil.toString(cache.group.captain))
                {
                    MsgManager.addBroadCast("您不是队长，不能发出繁殖邀请！");
                    return false;
                }
            }
            if (this._friendOnline == false)
            {
                MsgManager.addBroadCast("对方不在线，不能发出繁殖邀请！");
                return false;
            }
            return true;
        }

        private function onCancelTeamFeed(event:DataEvent) : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.PetTeamSire, {oper:EPetSireOperation.EPetSireOperCancel, uid:""}));
            
        }

        public function getTeammateEntityId() : SEntityId
        {
            var _loc_1:SEntityId = null;
            var _loc_3:SPublicMiniPlayer = null;
            var _loc_2:int = 0;
            while (_loc_2 < cache.group.players.length)
            {
                
                _loc_3 = cache.group.players[_loc_2] as SPublicMiniPlayer;
                if (EntityUtil.toString(_loc_3.entityId) != EntityUtil.toString(cache.role.entityInfo.entityId))
                {
                    _loc_1 = _loc_3.entityId;
                }
                _loc_2++;
            }
            return _loc_1;
        }

        private function onPetTeamSireSuccess(event:DataEvent) : void
        {
            MsgManager.addBroadCast("组队繁殖成功");
            
        }

        private function onPetGetBackPet(event:DataEvent) : void
        {
            var _loc_2:* = event.data;
            GameProxy.petProxy.getBackPet(_loc_2);
            
        }

        private function onPetGetBackPetSuccess(event:DataEvent) : void
        {
            var _loc_2:* = event.data as int;
            if (_loc_2 == -1)
            {
                MsgManager.addBroadCast("您没有可领取的宠物");
            }
            else if (_loc_2 == 1)
            {
                MsgManager.addBroadCast("你的宠物栏位不足,无法领取宠物！");
            }
            else if (_loc_2 == 0)
            {
                MsgManager.addBroadCast("领取宠物成功");
            }
            
        }

        public function canOpenTeamSire() : Boolean
        {
            var _loc_1:SEntityId = null;
            var _loc_2:IEntity = null;
            var _loc_3:NPCInfo = null;
            var _loc_4:Point = null;
            var _loc_5:Number = NaN;
            if (cache.pet.isCanOpenTeamSire)
            {
                return true;
            }
            if (cache.group.captain == null)
            {
                MsgManager.addBroadCast("您还没有队伍，不能进行双人繁殖！");
                return false;
            }
            if (EntityUtil.toString(cache.role.entityInfo.entityId) != EntityUtil.toString(cache.group.captain))
            {
                MsgManager.addBroadCast("您不是队长，不能进行双人繁殖！");
                return false;
            }
            if (cache.group.players.length != 2)
            {
                MsgManager.addBroadCast("队伍人数必须为2人才能进行双人繁殖！");
                return false;
            }
            if (this._friendOnline == false)
            {
                MsgManager.addBroadCast("对方不在线，不能进行双人繁殖！");
                return false;
            }
            _loc_1 = this.getTeammateEntityId();
            if (_loc_1)
            {
                _loc_2 = ThingUtil.entityUtil.getEntity(_loc_1);
                if (_loc_2)
                {
                    _loc_3 = cache.dialog.npcInfo;
                    if (_loc_3)
                    {
                        _loc_4 = GameMapUtil.getPixelPoint(_loc_3.snpc.point.x, _loc_3.snpc.point.y);
                        _loc_5 = Math.sqrt(Math.pow(_loc_4.x - _loc_2.x, 2) + Math.pow(_loc_4.y - _loc_2.y, 2));
                        if (_loc_5 > 120)
                        {
                            MsgManager.addBroadCast("队友离NPC过远，不能进行双人繁殖！");
                            return false;
                        }
                    }
                }
            }
            return true;
        }

        private function onGirdWalkEnd(event:PlayerEvent) : void
        {
            var _loc_3:NPCInfo = null;
            var _loc_4:Point = null;
            var _loc_5:Number = NaN;
            var _loc_2:* = RolePlayer.instance.currentPoint;
            if (this._petForgeModule && this._petForgeModule.isHide == false)
            {
                _loc_3 = cache.dialog.npcInfo;
                if (_loc_3)
                {
                    _loc_4 = GameMapUtil.getPixelPoint(_loc_3.snpc.point.x, _loc_3.snpc.point.y);
                    _loc_5 = Math.sqrt(Math.pow(_loc_4.x - _loc_2.x, 2) + Math.pow(_loc_4.y - _loc_2.y, 2));
                    if (_loc_5 > 150)
                    {
                        this._petForgeModule.hide();
                    }
                }
            }
            
        }

        private function onPetSireOperAgreeHandler(param1:SPetSire = null) : void
        {
            cache.pet.isCanOpenTeamSire = true;
            PetForgeModule.currentForgeType = PetForgeType.teamBreed;
            toOpenTab = 3;
            this.view.show();
            cache.pet.isCanSelectPetSire = true;
            if (this._petForgeModule.teamFreedPanel)
            {
                this._petForgeModule.teamFreedPanel.clearPanel();
            }
            
        }

        private function getFriendPetSireInfo(param1:SPetSire) : SPetSireInfo
        {
            var _loc_2:SPetSireInfo = null;
            if (param1 && param1.fromEntity)
            {
                if (EntityUtil.toString(param1.fromEntity) == EntityUtil.toString(param1.toInfo.entityId))
                {
                    _loc_2 = param1.toInfo;
                }
                else
                {
                    _loc_2 = param1.fromInfo;
                }
            }
            return _loc_2;
        }

        private function onPetSireOperUpdatePetHandler(param1:SPetSire = null) : void
        {
            var _loc_3:SPetSireInfo = null;
            var _loc_2:* = new SPetInfo();
            if (param1)
            {
                _loc_3 = this.getFriendPetSireInfo(param1);
                _loc_2.uid = _loc_3.uid;
                _loc_2.growth = _loc_3.growth;
                _loc_2.sex = _loc_3.sex;
                _loc_2.code = _loc_3.itemCode;
                _loc_2.icon = _loc_3.icon;
                this._petForgeModule.teamFreedPanel.update({petInfo:_loc_2, pos:"RIGHT"});
            }
            
        }

        private function onPetSireOperLockHandler(param1:SPetSire = null) : void
        {
            this._petForgeModule.teamFreedPanel.lockRightLock();
            MsgManager.addBroadCast("对方已锁定");
            
        }

        private function onPetSireOperConfirmHandler(param1:SPetSire = null) : void
        {
            MsgManager.addBroadCast("对方已确认繁殖");
            
        }

        private function onPetSireOperCancelHandler(param1:SPetSire = null) : void
        {
            cache.pet.isCanOpenTeamSire = false;
            if (this._petForgeModule.teamFreedPanel)
            {
                this._petForgeModule.teamFreedPanel.clearPanel();
                if (EntityUtil.toString(param1.fromEntity) == EntityUtil.toString(cache.role.entityInfo.entityId))
                {
                    MsgManager.addBroadCast("您已取消繁殖");
                }
                else
                {
                    MsgManager.addBroadCast("对方已取消繁殖");
                }
                this._petForgeModule.cancelPetSelect();
            }
            cache.pet.hadCancel = true;
            cache.pet.isCanSelectPetSire = false;
            
        }

        private function onPetSireOperEndHandler(param1:SPetSire = null) : void
        {
            cache.pet.isCanOpenTeamSire = false;
            if (this._petForgeModule && this._petForgeModule.teamFreedPanel)
            {
                this._petForgeModule.teamFreedPanel.clearPanel();
                this._petForgeModule.hide();
            }
            if (param1 && param1.fromInfo.status == EPetSireStatus.EPetSireStatusSuccess && param1.toInfo.status == EPetSireStatus.EPetSireStatusSuccess)
            {
                MsgManager.addBroadCast("您的宠物开始进入繁殖状态");
            }
            else
            {
                MsgManager.addBroadCast("繁殖结束");
            }
            
        }

        private function onGroupDownline(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SEntityId;
            if (this.getTeammateEntityId() && _loc_2)
            {
                if (EntityUtil.toString(this.getTeammateEntityId()) == EntityUtil.toString(_loc_2))
                {
                    this._friendOnline = false;
                }
            }
            if (PetForgeModule.currentForgeType == PetForgeType.teamBreed)
            {
                this._petForgeModule.teamFreedPanel.clearPanel();
                MsgManager.addBroadCast("对方不在线，无法繁殖宠物");
            }
            
        }

        private function onGroupOnline(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SEntityId;
            if (this.getTeammateEntityId() && _loc_2)
            {
                if (EntityUtil.toString(this.getTeammateEntityId()) == EntityUtil.toString(_loc_2))
                {
                    this._friendOnline = true;
                }
            }
            
        }

        private function onGroupDisband(event:DataEvent) : void
        {
            if (PetForgeModule.currentForgeType == PetForgeType.teamBreed)
            {
                this._petForgeModule.teamFreedPanel.clearPanel();
                MsgManager.addBroadCast("你与对方不在同一队伍中，无法繁殖宠物！");
                this._petForgeModule.hide();
            }
            
        }

        private function onPetOpenGrowthBlessWindowHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data["petInfo"] as SPetInfo;
            var _loc_3:* = event.data["blessLevel"] as int;
            if (this._growthBlessWindow == null)
            {
                this._growthBlessWindow = new PetGrowthBlessWindow(_loc_2);
            }
            this._growthBlessWindow.blessLevel = _loc_3;
            this._growthBlessWindow.updateByPetInfo(_loc_2);
            this._growthBlessWindow.show();
            
        }

        private function onPetOpenSpiritBlessWindowHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data["petInfo"] as SPetInfo;
            var _loc_3:* = event.data["blessLevel"] as int;
            if (this._spiritBlessWindow == null)
            {
                this._spiritBlessWindow = new PetSpiritBlessWindow(_loc_2);
            }
            this._spiritBlessWindow.blessLevel = _loc_3;
            this._spiritBlessWindow.updateByPetInfo(_loc_2);
            this._spiritBlessWindow.show();
            
        }

        private function onPetUseBlessHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data["uid"] as String;
            var _loc_3:* = event.data["isGrowth"] as Boolean;
            GameProxy.petProxy.usePetBless(_loc_2, _loc_3);
            
        }

        private function onPetUseBlessSuccessHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as Boolean;
            if (_loc_2)
            {
                MsgManager.addBroadCast("接受成长祝福成功");
            }
            else
            {
                MsgManager.addBroadCast("接受灵性祝福成功");
            }
            
        }

        public function getShowGrowthBlessWindowPetUid() : String
        {
            var _loc_1:String = null;
            if (this._growthBlessWindow != null && this._growthBlessWindow.isHide == false)
            {
                if (this._growthBlessWindow.petInfo)
                {
                    _loc_1 = this._growthBlessWindow.petInfo.uid;
                }
            }
            return _loc_1;
        }

        public function getShowSpiritBlessWindowPetUid() : String
        {
            var _loc_1:String = null;
            if (this._spiritBlessWindow != null && this._spiritBlessWindow.isHide == false)
            {
                if (this._spiritBlessWindow.petInfo)
                {
                    _loc_1 = this._spiritBlessWindow.petInfo.uid;
                }
            }
            return _loc_1;
        }

    }
}
